<template>
  <div class="box">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator="/" class="user-tou">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 警告区域 -->

      <el-alert
        title="注意：只允许为第三级设置相关参数!"
        type="warning"
        :closable="false"
        show-icon
      />
      <!-- 渲染商品分类区域 -->
      <el-row class="cat_opt">
        <el-col>
          <span class="fen">选择商品分类</span>
          <!-- 选择商品级联选择器 -->
          <el-cascader
            v-model="selectedparams"
            :options="paramslist"
            :props="paramsprops"
            @change="paramschange"
          />
        </el-col>
      </el-row>
      <!-- tab页签 -->
      <el-tabs v-model="paramsName" @tab-click="paramsClick">
        <el-tab-pane label="动态参数" name="many">
          <!-- 添加参数按钮 -->
          <el-button
            type="primary"
            size="mini"
            :disabled="isbtnparams"
            @click="paramsVisible = true"
            >添加动态参数</el-button
          >
          <!-- 动态参数表格 -->
          <el-table :data="manyparms" border stripe>
            <!-- 展开列 -->
            <!-- <el-table-column type="expand"> </el-table-column> -->
            <!-- 索引列 -->
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="showparams(row.attr_id)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="remove(row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <!-- 静态属性 -->
        <el-tab-pane label="静态参数" name="only">
          <el-button
            type="primary"
            size="mini"
            :disabled="isbtnparams"
            @click="paramsVisible = true"
            >添加属性</el-button
          >
          <el-table :data="onlyparms" border stripe>
            <!-- 展开列 -->
            <!-- <el-table-column type="expand"> </el-table-column> -->
            <!-- 索引列 -->
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="showparams(row.attr_id)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="remove(row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加公共参数对话框 -->
    <el-dialog
      :title="'添加' + titleparms"
      :visible.sync="paramsVisible"
      width="50%"
      @close="paramsclosed"
    >
      <el-form
        :model="addform"
        label-width="100px"
        ref="addparmsref"
        :rules="addparmsrules"
      >
        <el-form-item :label="titleparms" prop="attr_name">
          <el-input v-model="addform.attr_name"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="paramsVisible = false">取 消</el-button>
        <el-button type="primary" @click="paramsdan">确 定</el-button>
      </div>
    </el-dialog>
    <!-- //修改公共对话框 -->
    <el-dialog
      :title="'修改' + titleparms"
      :visible.sync="edparamsVisible"
      width="50%"
      @close="edparamsclosed"
    >
      <el-form
        :model="edform"
        label-width="100px"
        ref="edparmsref"
        :rules="addparmsrules"
      >
        <el-form-item :label="titleparms" prop="attr_name">
          <el-input v-model="edform.attr_name"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="edparamsVisible = false">取 消</el-button>
        <el-button type="primary" @click="edparamsdan">确 定</el-button>
      </div>
      <!--  -->
    </el-dialog>
  </div>
</template>

<script>
import {
  getparams,
  lieparams,
  tianparams,
  bianparams,
  tiparams,
  shanparams,
} from "@/api/params";
export default {
  name: "LoginIndex",
  data() {
    return {
      // 商品分类列表
      paramslist: [],
      // 选择框配对象
      paramsprops: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children",
      },
      // 级联选择框双向绑定的数组
      selectedparams: [],
      // 被激活的页签名称
      paramsName: "many",
      //动态参数数据   判断获取数据进行判断是动还是静 分别保存
      manyparms: [],
      //静态参数数据
      onlyparms: [],
      //控制添加对话框显示隐藏
      paramsVisible: false,
      //添加表单内容
      addform: {
        attr_name: "",
      },
      //添加表单验证规则
      addparmsrules: {
        attr_name: [
          { required: true, message: "请输入参数名称", trigger: "blur" },
        ],
      },
      //修改弹框的显示和隐藏
      edparamsVisible: false,
      //修改表单数据对象
      edform: {
        attr_name: "",
      },
    };
  },
  created() {
    this.getparms();
  },
  computed: {
    //如果按钮需要被禁用则返回true否则返回false
    isbtnparams() {
      //数组长度为3，启用数组长度不等于3，禁用
      if (this.selectedparams.length !== 3) {
        return true;
      }
      return false;
    },
    //当前选中三级分类的id
    cateId() {
      if (this.selectedparams.length === 3) {
        return this.selectedparams[2];
      }
      return null;
    },
    // 动态计算标题文本
    titleparms() {
      if (this.paramsName === "many") {
        return "动态参数";
      }
      return "静态参数";
    },
  },
  methods: {
    // 获取所有商品分类
    async getparms() {
      const res = await getparams();
      // console.log(res);
      this.paramslist = res.data;
    },
    //级联选择框选中变化会触发这个函数
    paramschange() {
      this.getparamsdata();
    },
    // tab点击事件处理函数
    paramsClick() {
      // console.log(this.paramsName);
      this.getparamsdata();
    },
    //获取参数列表数据
    async getparamsdata() {
      if (this.selectedparams.length !== 3) {
        this.selectedparams = [];
        return;
      }
      // 证明选择是三级分类
      // console.log(this.selectedparams);

      //根据所选的分类id，和当前所处面板获取对应参数
      const res = await lieparams(this.cateId, {
        sel: this.paramsName,
      });
      console.log(res);
      //根据当前点击的属性paramsName是many还是only区分静动 再把数据给到点击的参数 下步进行渲染
      if (this.paramsName === "many") {
        this.manyparms = res.data;
      } else {
        this.onlyparms = res.data;
      }
    },
    //监听添加对话框关闭事件
    paramsclosed() {
      this.$refs.addparmsref.resetFields();
    },
    //点击按钮添加参数
    paramsdan() {
      this.$refs.addparmsref.validate(async (valid) => {
        if (!valid) {
          return;
        }
        await tianparams(this.cateId, {
          attr_name: this.addform.attr_name,
          attr_sel: this.paramsName,
        });
        // console.log(res);

        this.$message.success("添加参数成功");
        this.getparamsdata();
        this.paramsVisible = false;
      });
    },
    //点击按钮展示修改对话框
    async showparams(attr_id) {
      //查询当前参数信息
      // console.log(attr_id);
      const { data: res } = await bianparams(this.cateId, attr_id, {
        params: { attr_sel: this.paramsName },
      });
      // console.log(res);

      this.edform = res;
      this.edparamsVisible = true;
    },
    //重置修改表单
    edparamsclosed() {
      this.$refs.edparmsref.resetFields();
    },
    //点击按钮，修改参数信息
    edparamsdan() {
      this.$refs.edparmsref.validate(async (valid) => {
        if (!valid) {
          return;
        }
        const res = await tiparams(this.cateId, this.edform.attr_id, {
          attr_name: this.edform.attr_name,
          attr_sel: this.paramsName,
        });
        // console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("修改参数失败");
        }
        this.$message.success("修改参数成功");
        this.getparamsdata();
        this.edparamsVisible = false;
      });
    },
    //根据Id删除对应参数数据
    async remove(attr_id) {
      const confirmparams = await this.$confirm(
        "此操作将永久删除参数，是否继续？",
        {
          confirmButtonText: "确定",
          concelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //用户取消删除操作
      if (confirmparams !== "confirm") {
        return this.$message.info("已取消删除！");
      }
      //用户点击删除操作
      await shanparams(this.cateId, attr_id);
      // console.log(res);
      this.$message.success("删除成功");
      this.getparamsdata();
    },
  },
};
</script>

<style lang="less" scoped>
.user-tou {
  margin-bottom: 10px;
}
.cat_opt {
  margin: 15px 0;
}
.fen {
  margin-right: 10px;
}
</style>
